//数字滚动
;(function($, window, document) {
    "use strict";
    var defaults = {
        deVal: 0,       //传入值
        className:'dataNums',   //样式名称
        digit:''    //默认显示几位数字
    };
    function rollNumDaq(obj, options){
        this.obj = obj;
        options.deVal=this.toMoney(options.deVal,'',0);
        this.options = $.extend(defaults, options);
        this.init = function(){
            this.initHtml(obj,this.options);
        };
        this.scroToNum=function(num){
            var toNum=num?this.toMoney(num,'',0):this.options.deVal;
            this.options.deVal=toNum;
            if(obj.find('.dataOne').length==toNum.length){
                this.scroNum(this.options);
            }else{
                this.init();
            }

        }
    }
    rollNumDaq.prototype = {
        initHtml: function(obj,options){
            var strHtml = '<ul class="' + options.className + ' inrow">';
            var valLen = options.digit ||  (options.deVal + '').length;
            if(obj.find('.'+options.className).length != valLen){
                for(var i = 0; i<  valLen; i++){
                    if(options.deVal[i]!=','){
                        strHtml += '<li class="dataOne "><div class="dataBoc">' +
                        '<div class="tt" t="38">' +this.getNumHtml()+this.getNumHtml()+
                        '</div>' +
                        '</div></li>';
                    }else{
                        strHtml += '<li class="dataOne symbol"><div class="dataBoc">' +
                        '<div class="tt" t="38"><span class="num0" text=",">&#44;</span></div>' +
                        '</div></li>';
                    }

                }
                strHtml += '</ul>';
                obj.html(strHtml);
            }
            this.scroNum(options);
        },
        getNumHtml:function(){
            var html='';
            for(var i=0;i<10;i++){
                html+='<span class="num'+i+'" text="'+i+'">'+i+'</span>'
            }
            return html;
        },
        scroNum: function(options){
            var number = options.deVal;
            var $num_item = $('.' + options.className).find('.tt');
            var h = $('.dataBoc').height();
            $num_item.css('transition','all 2s ease-in-out');
            var numberStr = number.toString();
            if(numberStr.length <= $num_item.length - 1){
                var tempStr = '';
                for(var a = 0; a < $num_item.length - numberStr.length; a++){
                    tempStr += '0';
                }
                numberStr = tempStr + numberStr;
            }

            var numberArr = numberStr.split('');
            $num_item.each(function(i, item) {
                setTimeout(function(){
                    $num_item.eq(i).css('top',-parseInt(numberArr[i])*h - h*10 + 'px');
                },i*100);
            });
        },
        toMoney:function(value, currency, decimals){

            var digitsRE = /(\d{3})(?=\d)/g;

            value = parseFloat(value);
            if (!isFinite(value) || (!value && value !== 0)) return '';
            currency = currency != null ? currency : '$';
            decimals = decimals != null ? decimals : 2;
            var stringified = Math.abs(value).toFixed(decimals);
            var _int = decimals
                ? stringified.slice(0, -1 - decimals)
                : stringified;
            var i = _int.length % 3;
            var head = i > 0
                ? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
                : '';
            var _float = decimals
                ? stringified.slice(-1 - decimals)
                : '';
            var sign = value < 0 ? '-' : '';
            return sign + currency + head +
                _int.slice(i).replace(digitsRE, '$1,') +
                _float
        }
    };
    $.fn.rollNumDaq = function(options){
        var $that = this;
        var rollNumObj = new rollNumDaq($that, options);
        rollNumObj.init();
        return rollNumObj;
    };
})(jQuery, window, document);